package com.sniff.atp.service.order;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.sniff.atp.dto.ClientLoginDTO;
import com.sniff.atp.dto.ContentDTO;
import com.sniff.atp.dto.SystemLoginDTO;
import com.sniff.atp.enums.api.OrderAPI;
import com.sniff.atp.service.login.SystemLoginService;
import com.sniff.atp.utils.Headers;
import com.sniff.atp.utils.HttpClientUtil;
import com.sniff.atp.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OEMOrderService {

    @Autowired
    private OrderService orderService;
    @Autowired
    private SystemLoginService systemLoginService;

    public String oemAddCart(ClientLoginDTO clientLoginDTO, Object oemGoods){
        return new HttpClientUtil(Headers.setClientHeader(clientLoginDTO))
                .doPostJson(OrderAPI.BATCH_ADD_CART.getApi(), oemGoods);
    }

    public String oemAddCart(ClientLoginDTO clientLoginDTO, String productSku){
        String getMyInventory = orderService.clientGetMyInventory(clientLoginDTO, productSku, "");
        String searchSourceOrderCode = JsonUtil.extractString(getMyInventory, "data.records[0].searchSourceOrderNo");
        String searchSourceOrderId = JsonUtil.extractString(getMyInventory, "data.records[0].searchSourceOrderId");
        JSONArray oemGoods = JsonUtil.readJSONArray("order/BatchOEMGoods", new ContentDTO("productSku", productSku),
                new ContentDTO("searchSourceOrderCode", searchSourceOrderCode), new ContentDTO("searchSourceOrderId", searchSourceOrderId));
        oemAddCart(clientLoginDTO, oemGoods);
        String cartList = orderService.getCurrentCartList(clientLoginDTO);
        JSONObject cartGoods = JsonUtil.findJSONArray(JsonUtil.extractJSONArray(cartList, "data.currentCartList"), "productCode", productSku);
        return cartGoods.getString("cartId");
    }

    /**
     * 创建OEM寻源单
     * @return searchSourceOrderId
     */
    public String createByVo(ClientLoginDTO clientLoginDTO, Integer oemProductClassificationId){
        JSONObject params;
        if(oemProductClassificationId==1){
            params = JsonUtil.readJson("oem/CreateByVoA");//服装
        }else if(oemProductClassificationId==2){
            params = JsonUtil.readJson("oem/CreateByVoB");//杂货
        }else{
            params = JsonUtil.readJson("oem/CreateByVoC");//辅料
        }
        String res = new HttpClientUtil(Headers.setClientHeader(clientLoginDTO))
                .doPostJson(OrderAPI.CREATE_BY_VO.getApi(), params); // /order/searchSourceOrder/operate/createByVo
        return JsonUtil.extractString(res, "data");
    }

    /**
     * 发布OEM寻源单
     */
    public void publish(ClientLoginDTO clientLoginDTO, String searchSourceOrderId){
        Map<String, String> params = new HashMap<>();
        params.put("customerShopID", clientLoginDTO.getShopId());
        params.put("searchSourceOrderId", searchSourceOrderId);
        new HttpClientUtil(Headers.setClientHeader(clientLoginDTO))
                .doGet(OrderAPI.PUBLISH.getApi(), params);
    }

    /**
     * 查询详情寻源单
     */
    public JSONObject searchSourceOrderDetail(SystemLoginDTO systemLoginDTO, String searchSourceOrderId){
        Map<String, String> params = new HashMap<>();
        params.put("searchSourceOrderId", searchSourceOrderId);
        String res = new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO))
                .doGet(OrderAPI.SEARCH_SOURCE_ORDER_DETAIL.getApi(), params);
        return JSON.parseObject(res);
    }

    /**
     * 翻译OEM寻源单
     */
    public void userUpdateByVo(SystemLoginDTO systemLoginDTO, String searchSourceOrderId){
        JSONObject searchSourceOrder =searchSourceOrderDetail(systemLoginDTO, searchSourceOrderId);
        JSONObject params = searchSourceOrder.getJSONObject("data");
        params.remove("referenceSku");
        new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO))
                .doPostJson(OrderAPI.USER_UPDATE_BY_VO.getApi(), params);
    }

    /**
     * 接单OEM寻源单
     */
    public void acceptOrder(SystemLoginDTO systemLoginDTO, String searchSourceOrderId){
        new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO))
                .doPostJson(OrderAPI.ACCEPT_ORDER.getApi(), searchSourceOrderId);
    }

    /**
     * 分单OEM寻源单
     */
    public void distributeOrder(SystemLoginDTO systemLoginDTO, String searchSourceOrderId){
        //获取采购员用户id
        JSONObject req = JsonUtil.readJson("oem/findUserList");
        String  res = new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO))
                .doPostJson(OrderAPI.GET_USER_ID.getApi(), req);
        String userId = JsonUtil.extractString(res, "data.records[0].manageUserId");
        //分单OEM寻源单
        JSONObject params = new JSONObject();
        JSONArray userList =new JSONArray();
        JSONObject user = new JSONObject();
        user.put("userId", userId);
        user.put("userName", "郑文钢");
        userList.add(user);

        params.put("searchSourceOrderId", searchSourceOrderId);
        params.put("userList", userList);
        new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO))
                .doPostJson(OrderAPI.DISTRIBUTE_ORDER.getApi(), params);
    }

    /**
     * 采购报价OEM寻源单
     */
    public void batchReportPurchasePrice(String searchSourceOrderId){
        SystemLoginDTO systemLoginDTO = systemLoginService.login("18761895550", "888888");
        JSONArray params = JsonUtil.readJSONArray("oem/batchReportPurchasePrice");
        params.getJSONObject(0).put("searchSourceOrderId", searchSourceOrderId);
        params.getJSONObject(1).put("searchSourceOrderId", searchSourceOrderId);
        new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO))
                .doPostJson(OrderAPI.BATCH_REPORT_PURCHASE_PRICE.getApi(), params);
    }

    /**
     * 寻源单报价详情（后台）
     */
    public List<JSONObject> getSupplierPriceListWithStatus(SystemLoginDTO systemLoginDTO, String searchSourceOrderNo){
        JSONObject params = new JSONObject();
        int[] arr = {1, 3, 4, 6};
        params.put("searchSourceOrderNo", searchSourceOrderNo);
        params.put("statusList",arr);
        String res = new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO))
                .doPostJson(OrderAPI.GET_SUPPLIER_PRICE_LIST_WITH_STATUS.getApi(), params);
        JSONObject resJsonObject = JSON.parseObject(res);
        JSONArray currentPriceList = resJsonObject.getJSONObject("data").getJSONArray("currentPriceList");
        return (List<JSONObject>) currentPriceList.stream().collect(Collectors.toList());

    }

    /**
     * 审核报价通过（后台）
     */
    public void approvePurchasePricePass(SystemLoginDTO systemLoginDTO, String searchSourceOrderSupplierPriceId){
        JSONObject params = new JSONObject();
        params.put("searchSourceOrderSupplierPriceId", searchSourceOrderSupplierPriceId);
       new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO))
                .doPostJson(OrderAPI.APPROVE_PURCHASE_PRICE_PASS.getApi(), params);
    }

    /**
     * 保存销售价（后台）
     */
    public void doBatchSavePrice(SystemLoginDTO systemLoginDTO, JSONArray savePriceList){
        new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO))
                .doPostJson(OrderAPI.DO_BATCH_SAVE_PRICE.getApi(), savePriceList);
    }

    /**
     * 提交销售价（后台）
     */
    public void batchFillSellPrice(SystemLoginDTO systemLoginDTO, JSONArray savePriceList){
        new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO))
                .doPostJson(OrderAPI.BATCH_FILL_SELL_PRICE.getApi(), savePriceList);
    }

    /**
     * 复审通过（后台）
     */
    public void recheckPass(SystemLoginDTO systemLoginDTO, String searchSourceOrderSupplierPriceId){
        JSONObject params = new JSONObject();
        params.put("searchSourceOrderSupplierPriceId", searchSourceOrderSupplierPriceId);
        new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO))
                .doPostJson(OrderAPI.RECHECK_PASS.getApi(), params);
    }

    /**
     * 翻译报价（后台）
     */
    public void translate(SystemLoginDTO systemLoginDTO, String searchSourceOrderSupplierPriceId){
        JSONArray params = JsonUtil.readJSONArray("oem/translateOrderSupplierPrice");
        params.getJSONObject(0).put("searchSourceOrderSupplierPriceId", searchSourceOrderSupplierPriceId);
        new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO))
                .doPostJson(OrderAPI.TRANSLATE.getApi(), params);
    }

    /**
     * 提交报价给客户（后台）
     */
    public void ddReportSellPrice(SystemLoginDTO systemLoginDTO, String searchSourceOrderId){
        new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO))
                .doPostJson(OrderAPI.DD_REPORT_SELL_PRICE.getApi(), searchSourceOrderId);
    }
}
